package cn.ffcs.bpm.web;

import java.io.File;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionSupport;

import cn.ffcs.base.model.BaseEntity;
import cn.ffcs.base.web.BaseAction;
import cn.ffcs.bpm.model.NextTep;
import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpm.model.ProcessConfig;
import cn.ffcs.bpm.model.ProcessConfigDetail;
import cn.ffcs.bpm.model.ProcessStep;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpm.model.Transactor;
import cn.ffcs.bpm.model.TransactorType;
import cn.ffcs.bpm.service.BizProcessManageService;
import cn.ffcs.bpm.service.NextTepService;
import cn.ffcs.bpm.service.ProcTranRecordService;
import cn.ffcs.bpm.service.ProcessConfigDetailService;
import cn.ffcs.bpm.service.ProcessConfigService;
import cn.ffcs.bpm.service.ProcessStepService;
import cn.ffcs.bpm.service.TransactProcessStatusService;
import cn.ffcs.bpm.service.TransactorService;
import cn.ffcs.bpm.service.TransactorTypeService;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.businessrequest.service.BusinessRequestService;
import cn.ffcs.common.model.Attach;
import cn.ffcs.common.service.AttachService;
import cn.ffcs.organ.model.Person;
import cn.ffcs.organ.service.OrgRelaPersonService;
import cn.ffcs.security.model.User;
import cn.ffcs.util.context.ApplicationContextUtil;

/**
 * 
 * 业务流程ACTION<br/> 
 * 处理流程初始化等数据<table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class BizProcessManageAction<B extends BaseEntity> extends BaseAction<B>
{
    
	private B bizProcessManage;
    private BizProcessManageService<B> bizProcessManageService;
    /** 附件列表数据 **/
    private List<Attach> attachs;
    /** 附件服务 **/
    private AttachService attachService;
    /** 附件上传 **/
    private List<File> uploads;
    /** 上传附件名称 **/
    private List<String> fileNames;
    /** 上传附件名称 **/
    private List<String> uploadContentTypes;
    /** 流程办理POJO **/
    private TransactProcessStatus transactProcessStatus;
    /** 办理基本信息POJO **/
    private BusinessRequest businessRequest;
    /** 办理人信息POJO **/
    private Transactor transactor;
    /** 流程历史信息POJO **/
    private ProcTranRecord procTranRecord;
    
    /** 流程办理状态服务 **/
    private TransactProcessStatusService transactProcessStatusService;
    /** 业务需求基本信息服务 **/
    private BusinessRequestService businessRequestService;
    /** 流程节点服务 **/
    private ProcessConfigDetailService processConfigDetailService;
    /** 流程步骤服务 **/
    private ProcessStepService processStepService;
    /** 流程配置服务 **/
    private ProcessConfigService processConfigService;
    /** 办理人服务 **/
    private TransactorService transactorService;
    /** 办理人类型服务 **/
    private TransactorTypeService transactorTypeService;
    /** 办理人类型服务 **/
    private NextTepService nextTepService;
    /** 办理人类型服务 **/
    private ProcTranRecordService procTranRecordService;
    /** 组织服务 **/
    private OrgRelaPersonService orgRelaPersonService;
    /** 流程配置数据集 **/
    private List<ProcessConfig> processConfigs;
    /** 流程步骤数据集 **/
    private List<ProcessStep> processSteps;
    /** 办理人类型数据集 **/
    private List<Transactor> transactors;
    /** 办理人类型数据集 **/
    private List<TransactorType> transactorTypes;
    /** 下一节点数据集 **/
    private List<NextTep> nextTeps;
    /** 流程步骤数据集 **/
    private List<ProcessConfigDetail> processConfigDetails;
    /** 流程步骤数据集 **/
    private List<TransactProcessStatus> transactProcessStatusList;
    /** 流程步骤数据集 **/
    private List<ProcTranRecord> procTranRecords;
    /** 业务流程单数据集 **/
    private List<BusinessRequest> businessRequests;
    
    
    /**
     * 初始化流程信息
     */
    protected void initProcess()
    {
        if (businessRequest == null || businessRequest.getId() == null)
        {
            return;
        }
        businessRequest = (BusinessRequest) businessRequestService
                .findById(businessRequest.getId());
        Criterion cond1 = Restrictions.eq("refEntity", businessRequest
                .getClass().getName());
        Criterion cond2 = Restrictions.eq("refEntityId", businessRequest
                .getId());
        Criterion cond = Restrictions.and(cond1, cond2);
        transactProcessStatusList = transactProcessStatusService.findBy(cond);
        if (transactProcessStatusList != null
                && transactProcessStatusList.size() > 0)
        {
            transactProcessStatus = transactProcessStatusList.get(0);
        }
        
        
        
        bizProcessManageService.initProcess(baseEntity,baseEntity.getId().toString());
        
    }
    
    

    /**
     * 查询显示当前操作人员待办单
     * @return
     */
    protected String listTodo(){
        
        return SUCCESS;
    }

    /**
     * 查询显示当前操作人员待办单
     * @return
     */
    protected String listHasTodo(){
        
        return SUCCESS;
    }
    
    /**
     * 新增或修改操作
     * @param baseEntity
     */
    public String saveOrUpdate()
    {
        boolean flag=true;      //新增标识，true为新增，false为修改
        // 获取附件是否为发布
        String[] publishIDs = getContextObjects("publishIDs");
        String[] attachIDs = getContextObjects("attachIDs");
        // 删除附件
        String[] delIDs = getContextObjects("delIDs");
        User curUser = ApplicationContextUtil.getUserContextObject()
        .getCurrUser();
        if(businessRequest!=null&&businessRequest.getId()!=null){
            flag=false;
        }
        else{
            if (curUser != null)
            {
                transactProcessStatus.setCurrTransactor(curUser.getPerson().getName());
            }
        }
        // 保存数据
        businessRequestService.saveBusinessRequest(businessRequest, uploads,
                fileNames, publishIDs, attachIDs, delIDs);
        transactProcessStatus.setRefEntityId(businessRequest);
        transactProcessStatus.setRefEntity(businessRequest.getClass().getName());

       // baseService.saveOrUpdate(transactProcessStatus);
        if(flag){       //新增的时候，保存当前操作人为待办人;修改时不处理
            Transactor transactor = new Transactor();
            transactor.setTranType("1");
            transactor.setCurrProcess(transactProcessStatus);
            Person person = new Person();
            person.setId(new Integer(curUser.getPersonID()));
            transactor.setUserId(person);
            transactorService.save(transactor);
        }
        return SUCCESS;
       // return processBusinessRequest();
    }
    
    
    
    /**
     * 流程单提交
     * @return
     */
    protected String submit(){
        return SUCCESS;
    }
    
    /**
     * 流程单提交
     * @return
     */
    protected String goBack(){
        return SUCCESS;
    }
    
    
    
    
    /**
     * @return the bizProcessManageService
     */
    public BizProcessManageService<B> getBizProcessManageService()
    {
        return bizProcessManageService;
    }

    /**
     * @param bizProcessManageService the bizProcessManageService to set
     */
    public void setBizProcessManageService(
            BizProcessManageService<B> bizProcessManageService)
    {
        this.bizProcessManageService = bizProcessManageService;
    }
    public List<File> getUpload()
    {
        return uploads;
    }

    public void setUpload(List<File> uploads)
    {
        this.uploads = uploads;
    }

    public List<String> getUploadFileName()
    {
        return fileNames;
    }

    public void setUploadFileName(List<String> fileNames)
    {
        this.fileNames = fileNames;
    }



	public List<Attach> getAttachs() {
		return attachs;
	}



	public void setAttachs(List<Attach> attachs) {
		this.attachs = attachs;
	}



	public AttachService getAttachService() {
		return attachService;
	}



	public void setAttachService(AttachService attachService) {
		this.attachService = attachService;
	}



	public B getBizProcessManage() {
		return bizProcessManage;
	}



	public void setBizProcessManage(B bizProcessManage) {
		this.bizProcessManage = bizProcessManage;
	}



	public List<String> getFileNames() {
		return fileNames;
	}



	public void setFileNames(List<String> fileNames) {
		this.fileNames = fileNames;
	}



	public List<String> getUploadContentTypes() {
		return uploadContentTypes;
	}



	public void setUploadContentTypes(List<String> uploadContentTypes) {
		this.uploadContentTypes = uploadContentTypes;
	}



	public BusinessRequest getBusinessRequest() {
		return businessRequest;
	}



//	public void setBusinessRequest(BusinessRequest businessRequest) {
//		this.businessRequest = businessRequest;
//	}



	public List<BusinessRequest> getBusinessRequests() {
		return businessRequests;
	}



	public void setBusinessRequests(List<BusinessRequest> businessRequests) {
		this.businessRequests = businessRequests;
	}



	public BusinessRequestService getBusinessRequestService() {
		return businessRequestService;
	}



	public void setBusinessRequestService(
			BusinessRequestService businessRequestService) {
		this.businessRequestService = businessRequestService;
	}



	public List<NextTep> getNextTeps() {
		return nextTeps;
	}



	public void setNextTeps(List<NextTep> nextTeps) {
		this.nextTeps = nextTeps;
	}



	public NextTepService getNextTepService() {
		return nextTepService;
	}



	public void setNextTepService(NextTepService nextTepService) {
		this.nextTepService = nextTepService;
	}



	public OrgRelaPersonService getOrgRelaPersonService() {
		return orgRelaPersonService;
	}



	public void setOrgRelaPersonService(OrgRelaPersonService orgRelaPersonService) {
		this.orgRelaPersonService = orgRelaPersonService;
	}



	public List<ProcessConfigDetail> getProcessConfigDetails() {
		return processConfigDetails;
	}



	public void setProcessConfigDetails(
			List<ProcessConfigDetail> processConfigDetails) {
		this.processConfigDetails = processConfigDetails;
	}



	public ProcessConfigDetailService getProcessConfigDetailService() {
		return processConfigDetailService;
	}



	public void setProcessConfigDetailService(
			ProcessConfigDetailService processConfigDetailService) {
		this.processConfigDetailService = processConfigDetailService;
	}



	public List<ProcessConfig> getProcessConfigs() {
		return processConfigs;
	}



	public void setProcessConfigs(List<ProcessConfig> processConfigs) {
		this.processConfigs = processConfigs;
	}



	public ProcessConfigService getProcessConfigService() {
		return processConfigService;
	}



	public void setProcessConfigService(ProcessConfigService processConfigService) {
		this.processConfigService = processConfigService;
	}



	public List<ProcessStep> getProcessSteps() {
		return processSteps;
	}



	public void setProcessSteps(List<ProcessStep> processSteps) {
		this.processSteps = processSteps;
	}



	public ProcessStepService getProcessStepService() {
		return processStepService;
	}



	public void setProcessStepService(ProcessStepService processStepService) {
		this.processStepService = processStepService;
	}



	public ProcTranRecord getProcTranRecord() {
		return procTranRecord;
	}



	public void setProcTranRecord(ProcTranRecord procTranRecord) {
		this.procTranRecord = procTranRecord;
	}



	public List<ProcTranRecord> getProcTranRecords() {
		return procTranRecords;
	}



	public void setProcTranRecords(List<ProcTranRecord> procTranRecords) {
		this.procTranRecords = procTranRecords;
	}



	public ProcTranRecordService getProcTranRecordService() {
		return procTranRecordService;
	}



	public void setProcTranRecordService(ProcTranRecordService procTranRecordService) {
		this.procTranRecordService = procTranRecordService;
	}



	public Transactor getTransactor() {
		return transactor;
	}



	public void setTransactor(Transactor transactor) {
		this.transactor = transactor;
	}



	public List<Transactor> getTransactors() {
		return transactors;
	}



	public void setTransactors(List<Transactor> transactors) {
		this.transactors = transactors;
	}



	public TransactorService getTransactorService() {
		return transactorService;
	}



	public void setTransactorService(TransactorService transactorService) {
		this.transactorService = transactorService;
	}



	public List<TransactorType> getTransactorTypes() {
		return transactorTypes;
	}



	public void setTransactorTypes(List<TransactorType> transactorTypes) {
		this.transactorTypes = transactorTypes;
	}



	public TransactorTypeService getTransactorTypeService() {
		return transactorTypeService;
	}



	public void setTransactorTypeService(TransactorTypeService transactorTypeService) {
		this.transactorTypeService = transactorTypeService;
	}



	public TransactProcessStatus getTransactProcessStatus() {
		return transactProcessStatus;
	}



//	public void setTransactProcessStatus(TransactProcessStatus transactProcessStatus) {
//		this.transactProcessStatus = transactProcessStatus;
//	}



//	public List<TransactProcessStatus> getTransactProcessStatusList() {
//		return transactProcessStatusList;
//	}



//	public void setTransactProcessStatusList(
//			List<TransactProcessStatus> transactProcessStatusList) {
//		this.transactProcessStatusList = transactProcessStatusList;
//	}



	public TransactProcessStatusService getTransactProcessStatusService() {
		return transactProcessStatusService;
	}



	public void setTransactProcessStatusService(
			TransactProcessStatusService transactProcessStatusService) {
		this.transactProcessStatusService = transactProcessStatusService;
	}
    
}
